{% extends "base.html" %}
{% block ext_title %}
    <title>图书信息修改</title>
{% endblock %}
{% block content %}
    <div class="content-wrapper">
        <section class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-12">
                        <ol class="breadcrumb float-sm-left">
                            <li class="breadcrumb-item"><a href="{{url_for('book_list')}}">图书管理</a></li>
                            <li class="breadcrumb-item active">图书修改</li>
                        </ol>
                    </div>
                </div>
            </div>
        </section>
        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <!-- left column -->
                    <div class="col-md-12">
                        <!-- Horizontal Form -->
                        <div class="card card-info">
                            <div class="card-header">
                                <h3 class="card-title">图书信息修改</h3>
                            </div>
                            <!-- /.card-header -->
                            <!-- form start -->
                            <form class="form-horizontal" method="post" enctype="multipart/form-data" novalidate>
                                {{ form.csrf_token }}
                                <div class="card-body">
                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">图书名称</label>

                                        <div class="col-sm-10">
                                            <input type="text" class="form-control" id="name" name="name"
                                                   value="{{ book.name }}" placeholder="图书名称">
                                            <span style="color:red">{{ form.name.errors[0] }}</span>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">ISBN</label>
                                        <div class="col-sm-10">
                                            <input type="text" class="form-control" id="isbn" name="isbn"
                                                   value="{{ book.isbn }}" placeholder="请输入ISBN">
                                            <span style="color:red">{{ form.isbn.errors[0] }}</span>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">图书封面</label>
                                        <div class="col-sm-8">
                                            <div class="input-group">
                                                <div class="custom-file">
                                                    {{ form.photo }}
                                                    <label class="custom-file-label">选择图书封面</label>
                                                </div>
                                            </div>
{#                                            <!--<input type="file" name="photo"  value="{{ book.photo }}"  id="photo">-->#}
                                            {% if book.photo %}
                                                <img id="preview-image" src="{{ url_for('static',filename='media/')}}{{ book.photo }}"
                                                     style="width: 150px; height: 150px;"/>
                                            {% else %}
                                                <img id="preview-image" src="{{ url_for('static',filename='img/default-150x150.png')}}"
                                                     style="width: 150px; height: 150px;"/>
                                            {% endif %}
                                            <span style="color:red">{{ form.photo.errors[0] }}</span>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">价格</label>
                                        <div class="col-sm-10">
                                            <input type="text" class="form-control" id="price" name="price"
                                                   value="{{ book.price }}" placeholder="请输入价格">
                                            <span style="color:red">{{ form.price.errors[0] }}</span>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">出版社</label>
                                        <div class="col-sm-10">
                                            {{ form.publish }}
                                            <span style="color:red">{{ form.publish.errors[0] }}</span>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="inputtext" class="col-sm-2 col-form-label">作者</label>
                                        <div class="col-sm-10">
                                            {{ form.author }}
                                            <span style="color:red">{{ form.author.errors[0] }}</span>
                                        </div>
                                    </div>
                                </div>
                                <!-- /.card-body -->
                                <div class="card-footer">
                                    <button type="submit" class="btn btn-info">保存</button>
                                    <script>    var bookListUrl = "{{ url_for('pub_list') }}";
                                    </script>
                                    <button type="button" class="btn btn-default"
                                            onclick="javascript:window.location=bookListUrl">返回
                                    </button>
{#                                    <span style="color:red">{{ errors }}</span>#}
                                </div>
                                <!-- /.card-footer -->
                            </form>
                        </div>
                        <!-- /.card -->
                    </div>

                    <!--/.col (right) -->
                </div>
                <!-- /.row -->
            </div><!-- /.container-fluid -->
        </section>
    </div>
    <script language="JavaScript">
        $('[name="photo"]').bind('change', function () {
            var file = this.files[0];
            var rdr = new FileReader();
            rdr.onload = function () {
                $('#preview-image').attr('src', this.result);
            };
            rdr.readAsDataURL(file);
        });
    </script>
{% endblock %}